50. Pow(x, n)
https://leetcode-cn.com/problems/powx-n/
- 提交时间:2021-12-05 14:35:12
- 执行用时:0 ms, 在所有 Go 提交中击败了100.00%的用户
- 内存消耗:2 MB, 在所有 Go 提交中击败了99.87%的用户
- 通过测试用例:304 / 304
func myPow(x float64, n int) (ans float64) {
if n < 0 {
return 1.0 / quickMul(x, -n)
}
return quickMul(x, n)
}
func quickMul(x float64, n int) (ans float64) {
ans = 1
x_contribute := x
for ; n > 0; n /= 2 {
if n&1 > 0 {
ans *= x_contribute
// fmt.Printf("ans:%+v\n", ans)
}
x_contribute *= x_contribute
}
return
}